package com.cx.mall.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@ControllerAdvice //全局异常处理
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseVo<String> handleException(Exception e) {
        log.error("全局异常捕获",e);
        return ResponseVo.fail("全局异常"+e.getMessage());
    }
    @ExceptionHandler(RuntimeException.class)
    public ResponseVo<String> handleRuntimeException(RuntimeException e) {
        log.error("全局运行期异常捕获",e);
        return ResponseVo.fail("全局运行期异常"+e.getMessage());
    }
    @ExceptionHandler(NullPointerException.class)
    public ResponseVo<String> handleNullPointerException(NullPointerException e) {
        log.error("全局空指针异常捕获",e);
        return ResponseVo.fail("全局空指针异常"+e.getMessage());
    }
    
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseVo<String> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数异常捕获",e);
        return ResponseVo.fail("参数异常: " + e.getMessage());
    }
    @ExceptionHandler(NoResourceFoundException.class)
    public ResponseEntity<String> handleNoResourceFoundException(NoResourceFoundException e) {
        // 记录可疑请求
        log.warn("可疑的静态资源请求: {}", e.getMessage());

        // 返回404状态码，避免暴露系统信息
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
                .body("Resource not found");
    }
}
